package com.mate.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mate.model.json.Coming;
import com.mate.model.vo.MovieDetailVo;
import com.mate.model.vo.MovieVo;
import com.mate.model.vo.RankVo;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MovieInfo {

    /**
     * 获取即将上映
     */
    public static List<Coming.ComingBean> getComing() {
        try {
            String re = HttpsUtils.doGet("https://m.maoyan.com/ajax/comingList?ci=89&token=&limit=10&optimus_uuid=F1B0647090DE11EAB8825B7772488A8548F6A50A13A24E01981BA9FE464D5198&optimus_risk_level=71&optimus_code=10");
            if (re.contains("haspromotionTag")) {
                JSONObject coming = (JSONObject) JSON.parse(re);
                JSONArray integerList = JSON.parseArray(coming.getString("movieIds"));
                String ids = "";
                List<Coming.ComingBean> mainList = new ArrayList<>(integerList.size());
                for (int i = 0; i < integerList.size(); i++) {
                    ids += integerList.get(i) + ",";
                    if (i % 10 == 0 || i == integerList.size() - 1) {
                        ids = ids.substring(0, ids.length() - 1);
                        String url = "https://m.maoyan.com/ajax/moreComingList?ci=89&token=&limit=10&movieIds=" + ids + "&optimus_uuid=F1B0647090DE11EAB8825B7772488A8548F6A50A13A24E01981BA9FE464D5198&optimus_risk_level=71&optimus_code=10";
                        String result = HttpsUtils.doGet(url);
                        JSONObject jsonRe = (JSONObject) JSON.parse(result);
                        JSONArray jsonList = JSON.parseArray(jsonRe.getString("coming"));
                        mainList.addAll(jsonList.toJavaList(Coming.ComingBean.class));
                        ids = "";
                    }
                    if (i > 50) {
                        break;
                    }
                }
                for (Coming.ComingBean ss : mainList) {
                    ss.setImg(checkPic(ss.getImg()));
                }
                return mainList;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取经典电影
     */
    public static List<MovieVo> getAlready() {
        try {
            int offset = 0;
            String re = "1";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
            SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
            List<MovieVo> mainList = new ArrayList<>(200);
            while (StringUtils.isNotBlank(re)) {
                String url = "https://m.maoyan.com/ajax/moreClassicList?sortId=1&showType=3&limit=50&optimus_uuid=F1B0647090DE11EAB8825B7772488A8548F6A50A13A24E01981BA9FE464D5198&optimus_risk_level=71&optimus_code=10&offset=" + offset;
                re = HttpsUtils.doGet(url);
                Document document = Jsoup.parse(re);
                Elements postList = document.getElementsByTag("a");
                //循环处理每篇博客
                for (Element postItem : postList) {
                    Integer id = Integer.valueOf(postItem.attr("href").replace("/movie/", ""));
                    String coverPic = postItem.getElementsByTag("img").attr("src");
                    String title = postItem.getElementsByClass("title").text();
                    String englishTitle = postItem.getElementsByClass("english-title").text();
                    String movieType = postItem.getElementsByClass("actors").text();
                    String comingTime = postItem.getElementsByClass("show-info").text();
                    String score = postItem.getElementsByClass("grade").text();
                    MovieVo movieVo = new MovieVo();
                    movieVo.setId(id);
                    movieVo.setCoverPic(checkPic(coverPic));
                    movieVo.setTitle(title);
                    movieVo.setEnglishTitle(englishTitle);
                    movieVo.setMovieType(movieType);
                    movieVo.setComingTime(comingTime);
                    movieVo.setScore(score);
                    mainList.add(movieVo);
                }
                /* 休息 1 秒 防止被屏蔽 */
                Thread.sleep(1000);
                offset += 50;
                if (offset > 200) {
                    re = "";
                }
            }
            return mainList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取票房排行
     */
    public static List<RankVo.MoviesBean> getRank() {
        try {
            String re = HttpsUtils.doGet("https://m.maoyan.com/board/1");
            int start = re.lastIndexOf("var AppData") + 14;
            int end = re.lastIndexOf(";</script>");
            re = re.substring(start, end);
            JSONObject jsonObject = (JSONObject) JSON.parse(re);
            RankVo rankVo = JSON.parseObject(jsonObject.getString("data"), RankVo.class);
            return rankVo.getMovies();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取想看电影排行
     */
    public static List<RankVo.MoviesBean> getRankClick() {
        try {
            String re = HttpsUtils.doGet("https://m.maoyan.com/board/3");
            int start = re.lastIndexOf("var AppData") + 14;
            int end = re.lastIndexOf(";</script>");
            re = re.substring(start, end);
            JSONObject jsonObject = (JSONObject) JSON.parse(re);
            RankVo rankVo = JSON.parseObject(jsonObject.getString("data"), RankVo.class);
            return rankVo.getMovies();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取电影详情
     */
    public static MovieDetailVo getDetail(String id) {
        try {
            String url = "https://m.maoyan.com/asgard/movie/" + id;
            String re = HttpsUtils.doGet(url);
            int start = re.lastIndexOf("var AppData") + 14;
            int end = re.lastIndexOf("// 灵犀埋点");
            re = re.substring(start, end);
            re = re.replace(";", "").replaceAll("<script>", "").replaceAll("</script>", "").trim();
            System.out.println(re);
            return JSON.parseObject(re, MovieDetailVo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 加工图片
     */
    public static String checkPic(String pic) {
        if (pic.contains("w.h/")) {
            pic = pic.replace("w.h/", "128.180");
        }
        return pic;
    }

    public static void main(String[] args) {
        getDetail("1292570");
    }
}
